package models

import (
	"fmt"
	"time"

	"github.com/astaxie/beego/logs"
)

//Time2Str format time to string
func Time2Str() string {
	const shortForm = "2006-01-02 15:04:05"
	t := time.Now()
	str := t.Format(shortForm)
	return str
}

//CheckToken Check whether the token is legal
func CheckToken(token string) bool {
	var ou OtherUser
	err := GetUserByToken(token, &ou)
	if err != nil {
		logs.Error("token value: ", token, ", token, is forged data, does not exist")
		return false
	}
	now := Time2Str()
	logs.Info("token: now: ", now, ",expir: ", ou.ExpirationTime)
	if now > ou.ExpirationTime {
		return false
	}
	return true
}

func OpenEulerScoreProc(openEulerScore float64) (CveLevel string) {
	if openEulerScore >= 9.0 {
		CveLevel = "Critical"
	} else if openEulerScore >= 7.0 && openEulerScore <= 8.9 {
		CveLevel = "High"
	} else if openEulerScore > 4.0 && openEulerScore <= 6.9 {
		CveLevel = "Medium"
	} else if openEulerScore <= 4.0 {
		CveLevel = "Low"
	}
	return CveLevel
}

func MindSporeScoreProc(openEulerScore float64) (CveLevel string) {
	if openEulerScore >= 9.0 {
		CveLevel = "Critical"
	} else if openEulerScore >= 7.0 && openEulerScore <= 8.9 {
		CveLevel = "High"
	} else if openEulerScore <= 7.0 {
		CveLevel = "Medium"
	}
	return CveLevel
}

func SearchGaussCondSa(cveLevel, years, releaseFlag int, searchName string) string {
	conditStr := ""
	if cveLevel > 0 {
		conditStr = fmt.Sprintf(`cve_level_value = %d`, cveLevel)
	}
	if years > 0 {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`sa_years = %d`, years)
		} else {
			conditStr = fmt.Sprintf(`sa_years = %d`, years)
		}
	}
	if releaseFlag == 2 {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`status = 3`)
		} else {
			conditStr = fmt.Sprintf(`status = 3`)
		}
	} else {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`status = 1`)
		} else {
			conditStr = fmt.Sprintf(`status = 1`)
		}
	}
	if len(searchName) > 1 {
		searchName = "%" + searchName + "%"
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`(gauss_sa_num like '%v' or influence_component like '%v')`,
				searchName, searchName)
		} else {
			conditStr = fmt.Sprintf(`gauss_sa_num like '%v' or influence_component like '%v'`, searchName, searchName)
		}
	}
	return conditStr
}

func SearchGaussCondCve(releaseFlag, isAffectFlag int, searchName string) string {
	conditStr := ""
	if len(searchName) > 1 {
		searchName = "%" + searchName + "%"
		conditStr = fmt.Sprintf(`cve_num like '%v'`, searchName)
	}
	if releaseFlag == 2 {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`status = 3`)
		} else {
			conditStr = fmt.Sprintf(`status = 3`)
		}
	} else {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`status = 1`)
		} else {
			conditStr = fmt.Sprintf(`status = 1`)
		}
	}
	if isAffectFlag == 3 {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`gauss_id = 0`)
		} else {
			conditStr = fmt.Sprintf(`gauss_id = 0`)
		}
	} else if isAffectFlag == 2 {
		if len(conditStr) > 1 {
			conditStr = conditStr + " and " + fmt.Sprintf(`gauss_id > 0`)
		} else {
			conditStr = fmt.Sprintf(`gauss_id > 0`)
		}
	}
	return conditStr
}
